package coupon

import (
	"context"
	"time"

	"go-weixin/app/preferential/api/internal/svc"
	"go-weixin/app/preferential/api/internal/types"
	"go-weixin/app/preferential/rpc/pb/proto/preferential"

	"github.com/zeromicro/go-zero/core/logx"
)

type CouponRegisterLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCouponRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CouponRegisterLogic {
	return &CouponRegisterLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CouponRegisterLogic) CouponRegister(req *types.CouponRegisterReq) (resp *types.CouponRegisterResp, err error) {
	ctx, channelFunc := context.WithTimeout(context.Background(), 5*time.Second)
	defer channelFunc()
	rpcResp, err := l.svcCtx.PreferentialRpc.CouponRegister(ctx, &preferential.CouponRegisterReq{
		Id:   req.Id,
		Name: req.Name,
	})
	if err != nil {
		return nil, err
	}
	resp = &types.CouponRegisterResp{
		Id:   rpcResp.Id,
		Name: rpcResp.Name,
	}
	return
}
